package com.edu.koTA07.utils;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.edu.koTA07.XmlDataSet.DiskonXmlDataSet;
import com.edu.koTA07.bean.DiskonBean;
import com.edu.koTA07.sqlHelper.SQLHelper;

public class DiskonUtils {
	@SuppressWarnings("unused")
	private static final String TAG = "cek";
	SQLHelper helper;
	SQLiteDatabase database;	
	
	public DiskonUtils(Context ctx) {
		helper = new SQLHelper(ctx);
		database = helper.getWritableDatabase();
	}
	
	@SuppressWarnings("static-access")
	public ArrayList<DiskonBean> loadData() {
		ArrayList<DiskonBean> currentData = new ArrayList<DiskonBean>();
		Cursor dataCursor = database.query(helper.TABLE_DISKON, 
				new String[] {helper.ID_DISKON, helper.ID_BARANG2, helper.BESAR_DISKON, helper.JUMLAH_BARANG}, 
				null, null, null, null, String.format("%s", helper.ID_DISKON));
		dataCursor.moveToFirst(); DiskonBean t;		
		if ( !dataCursor.isAfterLast() ) {
			do {
				String idDiskon = dataCursor.getString(0);
				String idBarang = dataCursor.getString(1); 
				int besarDiskon = dataCursor.getInt(2); 
				int jumlahBarang = dataCursor.getInt(3); 
								
				t = new DiskonBean(); 
				t.setIDDiskon(idDiskon);
				t.setIDBarang(idBarang);
				t.setBesarDiskon(besarDiskon);
				t.setJumlahBarang(jumlahBarang);  
				currentData.add(t);
				
			} while(dataCursor.moveToNext());
		}		
		dataCursor.close();		
		return currentData;
	}

	@SuppressWarnings("static-access")
	public void onSaveDataFromXml(DiskonXmlDataSet diskon) {
    	//assert (null != customer);
    	//helper.onCreate(database);
		
		for (int i = 0; i < diskon.getIDBarang().size(); i++) {
			ContentValues values = new ContentValues();
			values.put(helper.ID_DISKON, diskon.getIDDiskon().get(i));
			values.put(helper.ID_BARANG2, diskon.getIDBarang().get(i));
			values.put(helper.BESAR_DISKON, Integer.parseInt(diskon.getBesarDiskon().get(i)));
			values.put(helper.JUMLAH_BARANG, Integer.parseInt(diskon.getJumlahBarang().get(i)));
			database.insert(helper.TABLE_DISKON, null, values);
    	}
		
    }
	
	/*AlertDialog alert;
    public void onShowData(BarangBean brng, Context ctx) {
		final BarangBean thisItem = brng;		
		alert = new AlertDialog.Builder(ctx).setIcon(R.drawable.icon)
	        .setTitle("Display Data")
	        .setMessage(" ------------ Barang -------------\n"
	        		  + "ID: "+thisItem.getIDBarang()+"\n" 
	        		  + "Nama Barang: "+thisItem.getNamaBarang()+"\n"
	        		  + "Harga Barang: "+thisItem.getHargaBarang()+"\n")
	        .setNegativeButton("Close", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int whichButton) {
	               alert.cancel();
	            }
	        }).create();			
		alert.show();
	}*/

}
